<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <!--核心的样式文件-->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/ztree/css/zTreeStyle/zTreeStyle.css" />
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery1.12.4.js" ></script>
    <!--ztree枋心js文件-->
    <script type="text/javascript" src="${pageContext.request.contextPath}/ztree/js/jquery.ztree.core.js" ></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/ztree/js/jquery.ztree.excheck.min.js" ></script>
</head>
<body>

<h3>打牛蛙组购课系统树状菜单栏</h3>
<ul id="sampleTree" class="ztree">
</ul>

<script>
    /*
*  树形控件生成多级菜单
*/

    // 定义节点数据，采用简单数据格式(Array格式或isSampleData)
    var settins2 = {
        view: {
            showLine: false,  //去掉节点之间的关联线
            selectedMulti: false //禁止选择多个节点
        },
        data: {
            key: {
                //显示的名称(和数据库相同)
                name: "rightName"
            },
            simpleData: {
                enable: true, //开启简单数据格式
                idKey: "rightId", //主键id
                pIdKey: "rightSuperId" //父节点
            }
        },
        check: {
            enable: true, //启用ckeck功能
            //Y:勾选后的情况；N:取消勾选后的情况；p：父节点联动；s：子节点联动；"ps"父子节点都联动；""父子节点都不联动，只选中本节点
            chkboxType: {"Y": "ps", "N": "ps"}
        },
        callback: {
            //绑定函数
            // beforeClick:beforeClick,
            onCheck: onCheck
        }
    }
    // function findRights(){
    $.ajax({
        url: "${pageContext.request.contextPath}/roleController/findAllRights",
        dataType: "json",
        async: false,
        success: function (data) {
            //1、初始化树型菜单
            $.fn.zTree.init($("#sampleTree"), settins2, data).expandAll(true);
            //2、获取树型菜单，默认选中某些节点
            var zTree = $.fn.zTree.getZTreeObj("sampleTree");
            $.ajax({
                url: "${pageContext.request.contextPath}/roleController/bandChildRole",
                dataType: "json",
                async: false,
                success: function (data2) {
                    var zTree = $.fn.zTree.getZTreeObj("sampleTree");
                    $(data2).each(function (i, v) {
                        //查询节点
                        var node = zTree.getNodeByParam("id", v.rightId);
                        if (node != null) {
                            //选中
                            zTree.checkNode(node, true);
                        }
                    })
                }
            })
        }
    })
    // }
    /*
     * event:事件对象
     * treeId： 当前树的id属性值
     * treeNode： 当前树节点,通过当前节点可以获取所有的数据
     */
    function show(event, treeId, treeNode) {
        var id = treeNode.rightId;  //获取id的值
        var pId = treeNode.rightSuperId;  //获取pid的值
        var name = treeNode.rightName;  //获取name的值
        console.log(name);

    }

    /*function beforeClick(treeId, treeNode) {
        var zTree = $.fn.zTree.getZTreeObj("sampleTree");
        //勾选 或 取消勾选 单个节点
        //第3个参数 true 表示按照 setting.check.chkboxType 属性进行父子节点的勾选联动操作  false 表示只修改此节点勾选状态，无任何勾选联动操作
        zTree.checkNode(treeNode, !treeNode.checked, true, true);
        return true;
    }*/
    function onCheck(e, treeId, treeNode) {
        var treeObj = $.fn.zTree.getZTreeObj("sampleTree"),
            nodes = treeObj.getCheckedNodes(true);
        for (var i = 0; i < nodes.length; i++) {
            console.log("节点id:" + nodes[i].id + " 节点名称:" + nodes[i].rightSuperId + "父节点：" + nodes[i].rightSuperId); //获取选中节点的值
        }
    }

</script>

</body>
</html>
